package com.kk.crowd.mvc.controller;

import com.kk.crowd.entity.Auth;
import com.kk.crowd.entity.Role;
import com.kk.crowd.service.AdminService;
import com.kk.crowd.service.AuthService;
import com.kk.crowd.service.RoleService;
import com.kk.crowd.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/assign")
public class AssignController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private AuthService authService;

    @RequestMapping("/toAssignRolePage")
    public ModelAndView toAssignRolePage(@RequestParam("adminId") Integer adminId){

        //  1、查询已分配到的角色
        List<Role> assignedRoleList = roleService.getAssignedRole(adminId);

        //  2、查询未分配到的角色
        List<Role> unAssignedRoleList = roleService.getUnAssignedRole(adminId);

        //  3、存入ModelAndView
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("assignedRoleList",assignedRoleList);
        modelAndView.addObject("unAssignedRoleList",unAssignedRoleList);
        modelAndView.setViewName("assign-role");

        //  4、返回ModelAndView
        return modelAndView;
    }


    @RequestMapping("/doRoleAssign")
   public String saveAdminRoleRelationship(@RequestParam("adminId") Integer adminId,
                                                 @RequestParam("pageNum") Integer pageNum,
                                                 @RequestParam("keyword") String keyword,
                                                 //  roleIdList不是必须要有
                                                 @RequestParam(value = "roleIdList",required = false) List<Integer> roleIdList){

        adminService.saveAdminRoleRelationship(adminId, roleIdList);
        return "redirect:/admin/getAdminByPage?keyword="+keyword+"&pageNum="+pageNum;
   }


    @RequestMapping("/getAllAuth")
    @ResponseBody
    public ResultEntity getAllAuth(){
        List<Auth> authList = authService.getAllAuth();
        return ResultEntity.successWithData(authList);
    }

    @RequestMapping("/getAssignedAuthIdByRoleId")
    @ResponseBody
    public ResultEntity getAssignedAuthIdByRoleId(Integer roleId){
        List<Integer> authIdList = authService.getAssignedAuthIdByRoleId(roleId);
        return  ResultEntity.successWithData(authIdList);
    }


    @RequestMapping("/doRoleAssignAuth")
    @ResponseBody
    public ResultEntity doRoleAssignAuth(@RequestBody Map<String,List<Integer>> map){
        authService.saveRoleAuthRelationship(map);
        return ResultEntity.successWithoutData();
    }
}
